//
//  HMWelcomeViewController.swift
//  Weibo
//
//  Created by itteacher on 2017/7/25.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

//  欢迎界面
class HMWelcomeViewController: UIViewController {

    //  MARK: --懒加载控件
    fileprivate lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    fileprivate lazy var userImageView: UIImageView = {
        let imageView =  UIImageView(image: UIImage(named: "avatar_default_big"))
        if let imageUrl = HMUserAccountViewModel.sharedViewModel.userAccount?.avatar_large {
            imageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        //  圆角设置
        imageView.layer.cornerRadius = 45
        //  截去多余部分
        imageView.layer.masksToBounds = true
        return imageView
    }()
    //  消息内容
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel()
        if let name = HMUserAccountViewModel.sharedViewModel.userAccount?.name {
            label.text = "欢迎回来，\(name)"
        } else {
            label.text = "欢迎回来"
        }
        //  默认是透明
        label.alpha = 0
        //  设置字体大小
        label.font = UIFont.systemFont(ofSize: 12)
        //  文字颜色
        label.textColor = UIColor.darkGray
        return label
    }()
    
    //  自定义视图
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
    }
    //  视图已经显示完成
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startSpringAnimation()
    }
    
    
    //  添加控件设置约束
    private func setupUI() {
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        
        userImageView.snp.makeConstraints { (make) in
            make.top.equalTo(view).offset(200)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(10)
            make.centerX.equalTo(userImageView)
        }
    }
    
    //  开启弹簧动画
    private func startSpringAnimation() {
        //  更新约束要保证约束之前存在，否则更新崩溃
        userImageView.snp.updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        //  Damping: 阻尼 0-1
        //  Velocity: 初始速度
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: {
            //  执行的动画代码
            //  执行约束动画
            self.view.layoutIfNeeded()
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: { 
                self.messageLabel.alpha = 1.0
            }, completion: { (_) in
                //  进入首页
                
                //  切换根视图控制器
                NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotificationName), object: nil)
            })
            
        }
    
    }


}
